import gulp from 'gulp';
import gulpif from 'gulp-if';
import concat from 'gulp-concat'
import webpack from 'webpack'
import gulpWebpack from 'webpack-stream'
import named from 'vinyl-named'
import livereload from 'gulp-livereload'
import plumber from 'gulp-plumber'
import rename from 'gulp-rename'
import uglify from 'gulp-uglify'
import {log,colors} from 'gulp-util'
import args from './util/args'


gulp.task('scripts',()=>{
    return gulp.src(['app/js/index.js'])//打开文件
        .pipe(plumber({//错误处理
            errorHandler:function () {

            }
        }))
        .pipe(named())//重命名
        .pipe(gulpWebpack({//用webpack编译，以下是编译过程
            module:{//需要modules一个模块，然后loaders,如果遇到js的话，就用babel这个load去处理
                loaders:[{
                    test:/\.js$/,
                    loader:'babel-loader'
                }]
            }
        }),null,(err,stats)=>{//错误处理
            log(`Finished '${colors.cyan('scripts')}'`,stats.toString({
                chunks:false
            }))
        })
        .pipe(gulp.dest('server/public/js'))//上面经过打开，处理。这里是导出，将处理过的文件放入这个地方
        .pipe(rename({//将上文的东西重新命名，复制一份
            basename:'cp',
            extname:'min.js'
        }))
        .pipe(uglify({compress:{properties:false},output:{'quote_keys':true}}))//压缩
        .pipe(gulp.dest('server/public/js'))
        .pipe(gulpif(args.watch,livereload()))//当文件改变了以后自动刷新：使用gulpif做判断，执行livereload()

})

/*
import gulp from 'gulp';
import gulpif from 'gulp-if';
import concat from 'gulp-concat';
import webpack from 'webpack';
import gulpWebpack from 'webpack-stream';
import named from 'vinyl-named';
import livereload from 'gulp-livereload';
import plumber from 'gulp-plumber';
import rename from 'gulp-rename';
import uglify from 'gulp-uglify';
import {log,colors} from 'gulp-util';
import args from './util/args';

gulp.task('scripts',()=>{
    return gulp.src(['app/js/index.js'])
        .pipe(plumber({
            errorHandle:function(){

            }
        }))
        .pipe(named())
        .pipe(gulpWebpack({
            module:{
                loaders:[{
                    test:/\.js$/,
                    loader:'babel'
                }]
            }
        }),null,(err,stats)=>{
            log(`Finished '${colors.cyan('scripts')}'`,stats.toString({
                chunks:false
            }))
        })
        .pipe(gulp.dest('server/public/js'))
        .pipe(rename({
            basename:'cp',
            extname:'.min.js'
        }))
        .pipe(uglify({compress:{properties:false},output:{'quote_keys':true}}))
        .pipe(gulp.dest('server/public/js'))
        .pipe(gulpif(args.watch,livereload()))
})
*/

